

package ro.ase.catalog.security.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import ro.ase.catalog.beans.*;
import ro.ase.catalog.security.dao.*;
import ro.ase.catalog.security.filter.FilterSecurityInterceptor;
import ro.ase.catalog.security.filter.MethodSecurityInterceptor;
import ro.ase.catalog.security.service.ISecDataPrepareService;

import java.util.HashSet;
import java.util.List;


public class SecDataPrepareServiceImpl implements ISecDataPrepareService { 

    @Autowired
    private IUserDAO userDAO;
    @Autowired
    private IStudentDAO studentDAO;
    @Autowired
    private ITeacherDAO teacherDAO;

    @Autowired
    private IRoleDAO roleDAO;
    @Autowired
    private IAuthorityDAO authorityDAO;
    @Autowired
    private ISettingDAO settingDAO;
    @Autowired
    private IAclSidDAO aclSidDAO;
    @Autowired
    private IAclClassDAO aclClassDAO;
    @Autowired
    private IAclObjectIdentityDAO aclObjectIdentityDAO;
    @Autowired
    private IAclEntryDAO aclEntryDAO;
    
    @SuppressWarnings("serial")
    public void initDataForRun() {
    	List<Setting> settings = settingDAO.findAllDefault();
        if(settings !=null && settings.size() > 0) return;
        
        //insert  into setting(name,value,sortindex) values ('RowsPerPage','20',0);
        //insert  into setting(name,value,sortindex) values ('RowsPerPage','100',1);
        //insert  into setting(name,value,sortindex) values ('Skin','Blue',0);
        //insert  into setting(name,value,sortindex) values ('Skin','Gray',1);
        Setting setting = new Setting("RowsPerPage","20",0,true);
        settingDAO.save(setting);
        setting = new Setting("RowsPerPage","100",1,true);
        settingDAO.save(setting);
        setting = new Setting("Skin","Blue",0,true);
        settingDAO.save(setting);        
        setting = new Setting("Skin","Gray",1,true);
        settingDAO.save(setting);
        

        final Role roleAdmin = new Role("ROLE_ADMIN","Rol de administrare");
        roleDAO.save(roleAdmin);
        
        final Role roleTeacher= new Role("ROLE_TEACHER","Rol profesor");
        roleDAO.save(roleTeacher);

        final Role roleStudent= new Role("ROLE_STUDENT","Rol elev");
        roleDAO.save(roleStudent);

        //insert  into authorities(name,type,value) values ('All URL','URL','/**');
        //insert  into authorities(name,type,value) values ('/Security Modules/User List','URL','/security/userList.js*');
        //insert  into authorities(name,type,value) values ('/Security Modules/Authority List','URL','/security/authorityList.js*');
        //insert  into authorities(name,type,value) values ('/Security Modules/Role List','URL','/security/roleList.js*');
        //insert  into authorities(name,type,value) values ('/CRM Modules/Customer List','URL','/crm/customerList.js*');
        Authority authority = new Authority("All URL","URL","/**");
        authority.setRoles(new HashSet<Role>(){{add(roleAdmin);add(roleTeacher);add(roleStudent);}});
        authorityDAO.save(authority);

        authority = new Authority("/Securitate/Lista utilizatori","URL","/security/userList.js*");
        authority.setRoles(new HashSet<Role>(){{add(roleAdmin);}});
        authorityDAO.save(authority);

        authority = new Authority("/Securitate/Lista autoritati","URL","/security/authorityList.js*");
        authority.setRoles(new HashSet<Role>(){{add(roleAdmin);}});
        authorityDAO.save(authority);
        
        authority = new Authority("/Securitate/Lista roluri","URL","/security/roleList.js*");
        authority.setRoles(new HashSet<Role>(){{add(roleAdmin);}});
        authorityDAO.save(authority);

 //       authority = new Authority("Role List Search Method protect","METHOD","* IRoleService.*PerPage(..)");
   //   authority.setRoles(new HashSet<Role>(){{add(roleAdmin);add(roleTeacher);add(roleStudent);}});
      //  authorityDAO.save(authority);

        authority = new Authority("Role Bind Method protect","METHOD","* ro.ase.catalog.security.service.I*Service.*bind*(..)");
        authority.setRoles(new HashSet<Role>(){{add(roleAdmin);}});
        authorityDAO.save(authority);
        
        authority = new Authority("Role unBind Method protect","METHOD","* ro.ase.catalog.security.service.I*Service.*unBind*(..)");
        authority.setRoles(new HashSet<Role>(){{add(roleAdmin);}});
        authorityDAO.save(authority);
        
        authority = new Authority("add action of security module protect","METHOD","* ro.ase.catalog.security.service.I*Service.add*(..)");
        authority.setRoles(new HashSet<Role>(){{add(roleAdmin);}});
        authorityDAO.save(authority);
        
        authority = new Authority("update action of security module protect","METHOD","* ro.ase.catalog.security.service.I*Service.update*(..)");
        authority.setRoles(new HashSet<Role>(){{add(roleAdmin);}});
        authorityDAO.save(authority);

        authority = new Authority("delete action of security module protect","METHOD","* ro.ase.catalog.security.service.I*Service.delete*(..)");
        authority.setRoles(new HashSet<Role>(){{add(roleAdmin);}});
        authorityDAO.save(authority);

    //    authority = new Authority("update me method protect","METHOD","* IUserService.updateMe(..)");
      //  authority.setRoles(new HashSet<Role>(){{add(roleAdmin);add(roleTeacher);add(roleStudent);}});
      //  authority.setRoles(new HashSet<Role>(){{add(roleAdmin);add(roleTeacher);add(roleStudent);}});
    //    authorityDAO.save(authority);

        authority = new Authority("/Administrare/Profesori","URL","/catalog/teacherList.js*");
        authority.setRoles(new HashSet<Role>(){{add(roleAdmin);}});
        authorityDAO.save(authority);

        authority = new Authority("/Administrare/Elevi","URL","/catalog/studentList.js*");
        authority.setRoles(new HashSet<Role>(){{add(roleAdmin);}});
        authorityDAO.save(authority);
        
        //insert  into userbase(fullname,loginname,cryptpassword,emailaddress,enabled) values ('Administrator','admin','e10adc3949ba59abbe56e057f20f883e','administrator@catalog.ro',1);
        //insert  into userbase(fullname,loginname,cryptpassword,emailaddress,enabled) values ('Tommy Wang','user','e10adc3949ba59abbe56e057f20f883e','tommywang@catalog.ro',1);
        User user = new User("Administrator","admin","e10adc3949ba59abbe56e057f20f883e","test1.admin@gmail.com",true);
        user.setRoles(new HashSet<Role>(){{add(roleAdmin);}});
        userDAO.save(user);

        user = new User("Teacher","profesor","e10adc3949ba59abbe56e057f20f883e","test1.teacher@gmail.com",true);
        user.setRoles(new HashSet<Role>(){{add(roleTeacher);}});
        Teacher teacher=new Teacher("Profesor", "Test",user);
        teacherDAO.saveTransactionalTeacher(user, teacher);

        user = new User("Student","elev","e10adc3949ba59abbe56e057f20f883e","test1.elev@gmail.com",true);
        user.setRoles(new HashSet<Role>(){{add(roleStudent);}});
        Student student=new Student("Student", "Test","Str. X", "0213205373", user);
        studentDAO.saveTransactionalStudent(user, student);



        //insert into acl_sid(principal,sid) values (0,'ROLE_ADMIN');
        //insert into acl_sid(principal,sid) values (0,'ROLE_USER');
        
        AclSid aclSidAdmin = new AclSid(false,"ROLE_ADMIN");
        aclSidDAO.save(aclSidAdmin);

        AclSid aclSidUser = new AclSid(false,"ROLE_TEACHER");
        aclSidDAO.save(aclSidUser);

        AclSid aclSidStudent = new AclSid(false,"ROLE_STUDENT");
        aclSidDAO.save(aclSidStudent);
        
        //insert into acl_class(class) values ('Role');
        AclClass aclClass = new AclClass("Role");
        aclClassDAO.save(aclClass); 
        
        //insert into acl_object_identity(object_id_class,object_id_identity,owner_sid,entries_inheriting) 
        //values (1,1,1,1);
        //insert into acl_object_identity(object_id_class,object_id_identity,owner_sid,entries_inheriting) 
        //values (1,2,1,1);
        //insert into acl_object_identity(object_id_class,object_id_identity,owner_sid,entries_inheriting) 
        //values (1,3,1,1);
        AclObjectIdentity ao1 = new AclObjectIdentity(aclClass,roleAdmin.getId(),aclSidAdmin,true);
        aclObjectIdentityDAO.save(ao1);   
        AclObjectIdentity ao2 = new AclObjectIdentity(aclClass,roleTeacher.getId(),aclSidAdmin,true);
        aclObjectIdentityDAO.save(ao2);  
        AclObjectIdentity ao3 = new AclObjectIdentity(aclClass,roleStudent.getId(),aclSidAdmin,true);
        aclObjectIdentityDAO.save(ao3);  
        
        //insert into acl_entry(acl_object_identity,ace_order,sid,mask,granting,audit_success,audit_failure) 
        //values (1,1,1,16,1,0,0);
        //insert into acl_entry(acl_object_identity,ace_order,sid,mask,granting,audit_success,audit_failure) 
        //values (2,1,1,16,1,0,0);  
        //insert into acl_entry(acl_object_identity,ace_order,sid,mask,granting,audit_success,audit_failure) 
        //values (2,2,2,16,1,0,0); 
        //insert into acl_entry(acl_object_identity,ace_order,sid,mask,granting,audit_success,audit_failure) 
        //values (3,1,1,16,1,0,0); 
        
        AclEntry aclEntry = new AclEntry(ao1,1,aclSidAdmin,16,true,false,false);
        aclEntryDAO.save(aclEntry);   
        aclEntry = new AclEntry(ao2,1,aclSidAdmin,16,true,false,false);
        aclEntryDAO.save(aclEntry);   
        aclEntry = new AclEntry(ao2,2,aclSidUser,16,true,false,false);
        aclEntryDAO.save(aclEntry);
        aclEntry = new AclEntry(ao2,2,aclSidStudent,16,true,false,false);
        aclEntryDAO.save(aclEntry);
        aclEntry = new AclEntry(ao3,1,aclSidAdmin,16,true,false,false);
        aclEntryDAO.save(aclEntry);   
        
        FilterSecurityInterceptor.refresh();
        MethodSecurityInterceptor.refresh();
    }
}
